package com.web44.springboot.web;

import com.web44.springboot.bean.User;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLOutput;
import java.util.*;

/**
 * @Title: HelloController
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2024/2/28 9:23
 */
@Controller
public class HelloController {

    @GetMapping("hello")
    @ResponseBody
    public String helloWeb() {
        System.out.println(1/0);
        return "hello world";
    }

    @GetMapping("list")
    public String listWeb() {
        System.out.println(1/0);
        return "list";
    }

    /**
     *     @GetMapping("hello2") 请求的资源路径  /hello2
     *         return "hello";  视图
     *     因为配置了
     *       prefix = "classpath:/templates/";
     *       suffix = ".html";
     *     视图会前后拼接 最终的访问结果classpath:/templates/hello.html
     * @return
     */
    @GetMapping("hello.html")
    public String hello2Web() {

        return "hello";
    }

    /**
     * controller 默认接收
     *    HttpServletRequest
     *    HttpServletResponse
     *    HttpSession
     *    Model
     * @param model
     * @return
     */
    @GetMapping("hello02.html")
    public String hello02Web(Model model) {
        model.addAttribute("msg","hello thymeleaf!");
        return "hello02";
    }

    @GetMapping("hello03.html")
    public ModelAndView hello03Web() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello03");
        User user = new User();
        user.setName("小明");
        user.setAge(18);
        user.setFriend(new User("小红",20));
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @GetMapping("hello04.html")
    public ModelAndView hello04Web() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello04");
        User user = new User();
        user.setName("小明");
        user.setAge(18);
        user.setFriend(new User("小红",20));
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @GetMapping("hello05.html")
    public String hello05Web(HttpSession session,Model model) {
        User user = new User();
        user.setName("小明");
        user.setAge(18);
        user.setFriend(new User("小红",20));
        session.setAttribute("user",user);
        model.addAttribute("date",new Date());
        return "hello05";
    }

    @GetMapping("hello06.html")
    public ModelAndView hello06Web() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello06");
        User user = new User();
        user.setName("小明");
        user.setAge(18);
        user.setFriend(new User("小红",20));
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @GetMapping("hello07.html")
    public ModelAndView hello07Web() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello07");
        User user = new User();
        user.setName("小明");
        user.setAge(18);
        user.setFriend(new User("小红",20));
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @GetMapping("hello08.html")
    public ModelAndView hello08Web() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello08");
        List<User> list = Arrays.asList(
                new User("小明",18),
                new User("小红",19),
                new User("寒梅",20),
                new User("吉姆",21)
        );
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @GetMapping("hello09.html")
    public ModelAndView hello09Web() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello09");
        Map<String,User> map = new HashMap<>();
        map.put("1001", new User("小明",18));
        map.put("1002", new User("小红",19));
        map.put("1003", new User("寒梅",20));
        map.put("1004", new User("吉姆",21));

        modelAndView.addObject("map",map);
        return modelAndView;
    }

    @GetMapping("hello10.html")
    public ModelAndView hello10Web() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello10");
        User user = new User();
        user.setName("小明");
        user.setAge(18);
        user.setRole("admin1");
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @GetMapping("hello11.html")
    public ModelAndView hello11Web() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello11");
        User user = new User();
        user.setName("小明");
        user.setAge(18);
        user.setRole("admin1");
        user.setUrl("b.jpg");
        
        modelAndView.addObject("user",user);
        return modelAndView;
    }
}
